<?php
include_once("sessionmanager.php");
initSession();

if (!isset($_SESSION['s_loggedin']) || ($_SESSION['s_loggedin'] == false) || !isset($_SESSION['s_serverurl']) || (strpos($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'],$_SESSION['s_serverurl']) === false)) {
	$error_page_code = "timeout";
	include "errorpage.php";
	exit();
}
if (!$_SESSION['s_statusview']) {
	$error_page_code = "permission";
	include "errorpage.php";
	exit();
}

include "languages.php";
include_once "functions.inc.php";

include "config.php";
include "attributes.php";
$userid=$_SESSION['s_userid'];

makeheader(STATS_TITLE);


mysql_connect($dbhost,$dbusername,$dbpassword) or die(PARSER_ERROR3);
mysql_select_db($dbname) or die(PARSER_ERROR2);

if (!isset($_GET['show'])) $_GET['show'] = "player";

if (isset($_GET['ally']) && isset($_GET['status'])) {
	$status = mysql_real_escape_string($_GET['status']);
	$ally = mysql_real_escape_string(urldecode($_GET['ally']));
	$query = "UPDATE $astablename SET allystatus='$status' WHERE userid=$userid AND allyname='$ally'";
	mysql_query($query);
}


$page = (isset($_GET['page']) && is_numeric($_GET['page'])) ? (int)$_GET['page'] : 1;
$checked1 = "";
$checked2 = "";

if (isset($_GET['typ'])) {
	switch ($_GET['typ']) {
		case "frank"  : $order = "ORDER BY frank "; $rank = "frank"; break;
		case "rrank"  : $order = "ORDER BY rrank "; $rank = "rrank"; break;
		case "members" : $order = "ORDER BY members "; $rank = "members"; break;
		default       : $order = "ORDER BY rank "; $rank = "rank"; break;
	}
} else {
	$order = "ORDER BY rank ";
	$rank = "rank";
}
if (isset($_GET['how'])) {
	switch ($_GET['how']) {
		case "desc" : $order .= "DESC"; $how_link = "asc"; $how = "desc"; break;
		default     : $order .= "ASC"; $how_link = "desc"; $how = "asc"; break;
	}
} else {
	$order .= "ASC";
	$how_link = "desc";
	$how = "asc";
}

if (isset($_GET['show'])) {
	if ($_GET['show'] == "player") {
		$checked1 = "selected";
		$gesamtquery = "SELECT count(*) as anzahl FROM $pstablename WHERE userid=$userid AND rank > 0";
		$query = "SELECT * FROM $pstablename WHERE userid=$userid AND $rank > 0 $order LIMIT ".(($page-1)*$statspage).",".$statspage;
	}
	if ($_GET['show'] == "ally") {
		$checked2 = "selected";
		$gesamtquery = "SELECT count(*) as anzahl FROM $astablename WHERE userid=$userid AND rank > 0";
		$query = "SELECT * FROM $astablename WHERE userid=$userid AND $rank > 0 $order LIMIT ".(($page-1)*$statspage).",".$statspage;
	}
}

$res = mysql_query($gesamtquery);
$line = mysql_fetch_object($res);
$anzahl = $line->anzahl;

$select1 = '<select class="pulldown_code" name="show" onchange="stats.submit();">
<option value="player" '.$checked1.'>'.STATS_PLAYERSTATS.'</option>
<option value="ally" '.$checked2.'>'.STATS_ALLYSTATS.'</option>
</select>';
echo '<form name="stats" method="GET" action="stats.php">'."\n";
echo '<table cellpadding="4" cellspacing="0" border="0" width="90%" align="center">'."\n";
if ($_GET['show'] == "player") {
	echo '<tr class="tblhead"><td>'.STATS_TBLHEAD.' '.$select1.'</td></tr>'."\n";
	echo '<tr class="firstcolor"><td align="center">'."\n";
	if ($anzahl > $statspage) draw_pagenumbers($page,$anzahl,$statspage,$_SERVER['PHP_SELF']."?show=".$_GET['show']."&amp;typ=$rank&amp;how=$how","page");
	echo "</td></tr></table></form>\n";
	echo '<table cellpadding="4" cellspacing="2" border="0" width="90%" align="center">'."\n";
	echo '<tr class="tblhead">
    <td style="width:50px;" align="center">'.STATS_RANK.'</td>
    <td align="center">'.STATS_PLAYERNAME.'</td>
    <td align="center"><a class="link" href="stats.php?how='.(($rank == "rank") ? $how_link : "asc").'">'.STATS_SCORE.'</a></td>
    <td align="center"><a class="link" href="stats.php?typ=frank&amp;how='.(($rank == "frank") ? $how_link : "asc").'">'.STATS_FLEET.' ('.STATS_RANK.')</a></td>
    <td align="center"><a class="link" href="stats.php?typ=rrank&amp;how='.(($rank == "rrank") ? $how_link : "asc").'">'.STATS_RESEARCH.' ('.STATS_RANK.')</a></td>
    </tr>'."\n";

} else {
	echo '<tr class="tblhead"><td>'.STATS_TBLHEAD.' '.$select1.'</td></tr>'."\n";
	echo '<tr class="firstcolor"><td align="center">'."\n";
	if ($anzahl > $statspage) draw_pagenumbers($page,$anzahl,$statspage,$_SERVER['PHP_SELF']."?show=".$_GET['show']."&amp;typ=$rank&amp;how=$how","page");
	echo "</td></tr></table></form>\n";
	echo '<table cellpadding="4" cellspacing="2" border="0" width="90%" align="center">'."\n";
	echo '<tr class="tblhead">
    <td style="width:50px;" align="center">'.STATS_RANK.'</td>
    <td align="center">'.STATS_ALLYNAME.'</td>
    <td align="center"><a class="link" href="stats.php?show=ally&amp;typ=members&amp;how='.(($rank == "members") ? $how_link : "asc").'">'.STATS_MEMBERS.'</a></td>
    <td align="center">'.STATS_STATUS.'</td>
    <td align="center"><a class="link" href="stats.php?show=ally&amp;how='.(($rank == "rank") ? $how_link : "asc").'">'.STATS_SCORE.'</a></td>
    <td align="center"><a class="link" href="stats.php?show=ally&amp;typ=frank&amp;how='.(($rank == "frank") ? $how_link : "asc").'">'.STATS_FLEET.'</a></td>
    <td align="center"><a class="link" href="stats.php?show=ally&amp;typ=rrank&amp;how='.(($rank == "rrank") ? $how_link : "asc").'">'.STATS_RESEARCH.'</a></td>
    </tr>'."\n";
}

$res = mysql_query($query);
$i = 0;
while ($line = mysql_fetch_object($res)) {
	if ($_GET['show'] == "player") {
		echo '<tr class="firstcolor"><td align="center" onmouseover=\'this.T_WIDTH=150;return escape("'.STATS_UPDATE.": ".$line->last_update.'")\'>'.$line->$rank.'</td>
		      <td align="center"><a class="link" href="show.php?spieler='.urlencode($line->playername).'&new=true">'.$line->playername.'</a></td>
		   	  <td align="center">'.number_format($line->points,0,",",".").' '.(($rank == "rank") ? "" : "(".$line->rank.") ").'</td>
		      <td align="center">'.number_format($line->fpoints,0,",",".").' '.(($rank == "frank") ? "" : "(".$line->frank.") ").'</td>
		      <td align="center">'.number_format($line->rpoints,0,",",".").' '.(($rank == "rrank") ? "" : "(".$line->rrank.") ").'</td>
		      </tr>'."\n";
	} else {
		$selected = array("","","","","","","","");
		switch ($line->allystatus) {
			case "nothing": $selected[0] = "selected"; break;
			case "neutral": $selected[1] = "selected"; break;
			case "boycott": $selected[2] = "selected"; break;
			case "nap"    : $selected[3] = "selected"; break;
			case "ally"   : $selected[4] = "selected"; break;
			case "war"    : $selected[5] = "selected"; break;
			case "own"    : $selected[6] = "selected"; break;
			case "wing"   : $selected[7] = "selected"; break;
		}


		if ($rank == "members")
		echo '<tr class="firstcolor"><td align="center" onmouseover=\'this.T_WIDTH=150;return escape("'.STATS_UPDATE.": ".$line->last_update.'")\'>'.$line->rank.'</td>';
		else
		echo '<tr class="firstcolor"><td align="center" onmouseover=\'this.T_WIDTH=150;return escape("'.STATS_UPDATE.": ".$line->last_update.'")\'>'.$line->$rank.'</td>';
		echo '
	          <td align="center"><a class="link" href="show.php?ally='.urlencode($line->allyname).'&new=true">'.$line->allyname.'</a></td>
	          <td align="center">'.$line->members.'</td>';
		if ($line->allystatus != "nothing") {
			echo '<td align="center" class="bg_'.$line->allystatus.'">';
		} else {
			echo '<td align="center">';
		}
		if ($_SESSION['s_diplomatic']) {
			echo '<form name="changestats'.$i.'" method="GET" action="stats.php">'."\n";
			echo '<input type="hidden" name="ally" value="'.urlencode($line->allyname).'">
	              <input type="hidden" name="show" value="ally">'."\n";
			echo '<select class="pulldown_code" name="status" onchange="changestats'.$i.'.submit();">
	             <option class="nothing" value="nothing" '.$selected[0].'>'.STATS_NOTHING.'</option>
	             <option class="neutral" value="neutral" '.$selected[1].'>'.STATS_NEUTRAL.'</option>
	             <option class="boycott" value="boycott" '.$selected[2].'>'.STATS_BOYCOTT.'</option>
	             <option class="nap" value="nap" '.$selected[3].'>'.STATS_NAP.'</option>
	             <option class="ally" value="ally" '.$selected[4].'>'.STATS_ALLIED.'</option>
	             <option class="war" value="war" '.$selected[5].'>'.STATS_WAR.'</option>
	             <option class="own" value="own" '.$selected[6].'>'.STATS_OWN.'</option>
	             <option class="wing" value="wing" '.$selected[7].'>'.STATS_WING.'</option>
	             </select>';
			echo "</form>";
		} else {
			switch ($line->allystatus) {
				case "nothing": echo STATS_NOTHING; break;
				case "neutral": echo STATS_NEUTRAL; break;
				case "boycott": echo STATS_BOYCOTT; break;
				case "nap"    : echo STATS_NAP;     break;
				case "ally"   : echo STATS_ALLIED;  break;
				case "war"    : echo STATS_WAR;     break;
				case "own"    : echo STATS_OWN;     break;
				case "wing"   : echo STATS_WING;    break;
			}
		}
		echo '</td><td align="center">'.number_format($line->points,0,",",".").' '.(($rank == "rank") ? "" : "(".$line->rank.") ").'</td>
	          <td align="center">'.number_format($line->fpoints,0,",",".").' '.(($rank == "frank") ? "" : "(".$line->frank.") ").'</td>
	          <td align="center">'.number_format($line->rpoints,0,",",".").' '.(($rank == "rrank") ? "" : "(".$line->rrank.") ").'</td>
	          </tr>'."\n";

	}
	$i++;
}
echo "</table>\n";



makefooter();
?>
